package net.hnilica.emailer;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * <p>Title: ConnectionFactoryMyEdit.java</p>
 *
 * <p>Description: A class that other classes may use
 * to get a connection to the data storage.  This static
 * class manages the connection and associated resources.
 * We can use this calls to test connecting to the data store.</p>
 *
 * <p>Reference:  Pages 281-288 of Beginning Java Databases</p>
 *
 *
 * <p>Date: September 2005</p>
 *
 * @author Bruce Phillips
 *
 * @version 1.5
 */
public class ConnectionFactoryMyEdit {
        protected static Logger logStatic = Logger.getLogger("ConnectionFactory");
    protected Logger log = Logger.getLogger(getClass().getName());
    //Call the private constructor to initialize the DriverManager
    @SuppressWarnings("unused")
    private static ConnectionFactoryMyEdit ref = new ConnectionFactoryMyEdit();

    /**
     * Private default constructor
     * No outside objects can create
     * an object of this class
     * This constructor initializes the DriverManager
     * by loading the driver for the database
     */
    private ConnectionFactoryMyEdit() {

        String driverName = "org.postgresql.Driver";

        try {

            Class driver = Class.forName(driverName);

        } catch (ClassNotFoundException e) {

            log.log(Level.SEVERE, "ERROR: exception loading driver class", e);

        }//end try-catch

    } //end default private constructor

    /**
     * Get and return a Connection object
     * that can be used to connect to the
     * data storage
     * @return Connection
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {

        System.out.println("in get connection");//For test

        String dbName = "cistic";
        //Connect to the data storage
        //In this case it is a Derby database
        String sourceURL = "jdbc:postgresql://127.0.0.1:5432/" + dbName;
        String userName = "cistic_web";
        String password = "cistic_web";



        return DriverManager.getConnection(sourceURL, userName, password);

    } //end method getConnection

    /**
     * Close the ResultSet
     * @param rs ResultSet
     */
    public static void close(ResultSet rs) {

        try {

            rs.close();

        } catch (SQLException e) {


            logStatic.log(Level.SEVERE,"ERROR: Unable to close Result Set", e);

        } //end try-catch block

    } //end method close

    /**
     * Close statement object
     * @param stmt Statement
     */
    public static void close(Statement stmt) {

        try {

            stmt.close();

        } catch (SQLException e) {

            logStatic.log(Level.SEVERE,"ERROR: Unable to close Statement", e);
        } //end try-catch block

    } //end method close

    /**
     * Close connection
     * @param conn Connection
     */
    public static void close(Connection conn) {

        try {

            conn.close();

        } catch (SQLException e) {

            logStatic.log(Level.SEVERE,"ERROR: Unable to close Statement", e);

        } //end try-catch block

    } //end method close

} //end class ConnectionFactoryMyEdit

